var GlobalData = require('../../../../utils/GlobalData.js');
var dataManager = require('../../../../utils/dataManager.js');
var Util = require('../../../../utils/util.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phoneNum: 0,
    passwd1: "",
    passwd2: "",
    msg: "",
    pic: "",
    isShow: false,
    time: 0,
    btinfo: "获取验证码",
    messageTurn: true,
    // 获取验证码按钮
    turn: true,
    // 是否修改成功
    isSucee: false,
    timer: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },
  onClose: function () {
    this.setData({
      isShow: false
    })
  },
  /**获取验证码 */
  getMsg: function () {
    // console.log("获取验证码")
    if (!this.data.turn)
      return;
    if (!this.data.phoneNum || this.data.phoneNum < 10000000000 || this.data.phoneNum > 99999999999) {
      app.show("请输入正确的手机号。");
      return;
    }
let url = "";
    if (app.getConfig().isFormal){
      url = app.getConfig().url_code_formal
    }else{
      url = app.getConfig().url_code_debug
    }

    var imgurl = url + "?mobile=" + this.data.phoneNum + "&" + Math.random();
    if (imgurl) {
      this.setData({
        pic: imgurl,
        isShow: true
      })
    }

  },

  /**确认修改密码 */
  onConfirm: function () {
    // console.log("确认修改密码")
    var that = this
    if (that.data.isSucee)
      return;
    // console.log("确认修改密码", that.data.isSucee)
    if (!that.data.phoneNum) {
      app.show("请输入手机号码。");
      return;
    }
    else if (!that.data.msg) {
      app.show("请输入短信验证码。");
      return;
    }
    else if (!that.data.passwd1) {
      app.show("请输入新密码。");
      return;
    }
    else if (!that.data.passwd2) {
      app.show("请再次确认密码。");
      return;
    }
    else if (that.data.passwd1 != that.data.passwd2) {
      app.show("请保持两次密码一致。");
      return;
    }
    that.setData({
      isSucee: true
    })
     let url = app.getRequestUrl(app.getConfig().isFormal);
    wx.request({
      url: url,
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      method: "POST",
      data: Util.json2Form({ method: "modifypassword_forget", "mobile": that.data.phoneNum, "code": that.data.msg, "password": that.data.passwd1 }),
      complete: function (res) {
        let pkg = res.data;
        // console.log(res)
        if (res && pkg && pkg.errcode == 0) {
          that.setData({
            isSucee: false
          })
          app.onTip("修改密码成功", "/pages/login/login");
        } else {
          that.setData({
            isSucee: false
          })
          app.show(pkg.errmsg)
        }
      }
    })
  },

  /**手机号码 */
  bindKeyphone: function (e) {
    console.log(e)
    var num = parseInt(e.detail.value)
    if (num == 0) {
      return "";
    }
    this.setData({
      phoneNum: num
    })
  },

  /**密码 */
  bindKeypasswd1: function (e) {
    var code = e.detail.value.trim();
    console.log(code)
    this.setData({
      passwd1: code,
    })
  },
  /**密码 */
  bindKeypasswd2: function (e) {
    var code = e.detail.value.trim();
    console.log(code)
    this.setData({
      passwd2: code,

    })


  },
  onInputCode: function (e) {
    // console.log(e);
    var code = e.detail.value.trim();
    console.log(code)
    this.setData({
      msg: code,
    })
  },
    /**输入短信验证码 */
  bindKeyInput: function (e) {
    var code = e.detail.value.trim();
    console.log(code)
    this.setData({
      msg: code,
      // isShow: true
    })
  },
  /**输入验证码完成确认 */
  onSetWithoutCode: function () {

    var that = this;
    if (!that.data.msg || that.data.msg == "") {
      app.show("请输入验证码")
      return;
    }
    //判断验证码是否为空
    if (that.data.messageTurn) {
      that.setData({
        messageTurn: false,
        isShow: false
      })
       let url = app.getRequestUrl(app.getConfig().isFormal);
      wx.request({
        url: url,
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        method: "POST",
        data: Util.json2Form({ method: "sendcode_forgetpass", "mobile": that.data.phoneNum, "picturecode": that.data.msg }),
        complete: function (res) {
          let pkg = res.data;
          // console.log(res)
          if (res && pkg && pkg.errcode == 0) {
            that.setData({
              turn: false
            })
            app.show("验证码已发送到您的手机。");
            //1分钟限制
            that.setData({
              turn: false,
              btinfo: "60s",
              time: 60
            })
            // this.btinfo = "60s";
            // this.time = 60;
            that.data.timer = setInterval(() => {
              that.data.time = that.data.time - 1
              that.setData({
                btinfo: that.data.time + "s"
              })
              // that.btinfo = --that.data.time + "s";
              if (that.data.time == 0) {
                that.setData({
                  turn: true,
                  btinfo: "获取验证码",
                })
                clearInterval(that.data.timer);
              }
            }, 1000);
            // that.data.checkcode = "";
            that.setData({
              messageTurn: true,
              msg: ""
            })
          } else {
            that.setData({
              messageTurn: true
            })
            // that.data.messageTurn = true;
            app.show(pkg.errmsg)

          }
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})